$(function() {
	vm.queryPage();
});
var vm = new Vue({
	el : '#rrapp',
	data : {
		showList : true,
		flag : "disabled",
		title : null,
		showList : true,
		page : {
			pageSize : 20,
			currPage : 1,
			totalCount : 0,
			totalPage : 0,
		},
		superviseList : [],
		supervise : {},
		dim1 : [],
		dim2 : [],
		selected1 : '0',
		selected2 : '',
		selected3 : '',
		q : {
			post_time : '全部',
			post_time_from : '',
			post_time_to : '',
			platform : '',
			source : '',
			selectId : 0,
			selectName : '',
			thread_context : '',
		},
		show : {
			dim1 : [],
			dim2 : [],
			dim3 : [],
			dim4 : [],
			car1 : [],
			car2 : [],
			car3 : [],
			emotion1 : [],
			emotion2 : [],
			scenepattern1 : [],
			sentencepattern1 : [],
		},
		result : {
			docid : 0,
			index : 0,
			brand : '',
			series : '',
			model_num : '',
			first_dim : '',
			second_dim : '',
			third_dim : '',
			fourth_dim : '',
			emotion_word : '',
			emotion : '',
			emo_des : '',
			sen_demo : '',
			scene_demo : '',
		}
	},
	created : function() {
		this.getCarData(0);
		this.getDimData(0);
		this.getEmotionData(0);
		this.getScenepatternData();
		this.getSentencepatternData();
	},
	mounted : function() {
		this.initDateRang();
	},
	watch : {
		selected1 : function(parentId, oldVal) {
			this.selected2 = '';
			this.dim2 = [];
			if (parentId == '全部') {
				vm.selectId = 0;
			} else {
				$.get(baseURL + "carmodel/listfilter/" + parentId, function(r) {
					if (r) {
						vm.dim2 = r
						vm.q.selectId = parentId;
					}
				})
			}
		},
		selected2 : function(parentId, oldVal) {
			if (parentId == '') {
				vm.q.selectId = 0;
			} else {
				vm.q.selectId = parentId;
			}
			vm.page.currPage = 1;
			vm.queryPage();
		},
		page : function() {
		}
	},
	ready : function() {
		var tmp = 215;
		$('.h-3').each(function() {
			var height3 = $(this).height();
			$(this).children(':last-child').height(height3 - 1);
		})
		$('.h-0').each(function(i) {
			var length1 = $(this).children('.h-6').children().length;
			var height0 = $(this).height();
			if (length1 > 5) {
				$(this).children('.h-6').height(flag).css('overflow', 'auto');
				$(this).siblings().height(flag + 54).css('line-height', flag + 'px');
			} else {
				$(this).siblings().height(height0).css('line-height', height0 + 'px');
			}
		});
	},
	methods : {
		queryPage : function() {
			vm.q.pageSize = vm.page.pageSize;
			vm.q.currPage = vm.page.currPage;
			vm.q.totalCount = vm.page.totalCount;
			vm.q.totalPage = vm.page.totalPage;
			$.ajax({
				type : "POST",
				url : baseURL + "supervise/list",
				contentType : "application/json",
				data : JSON.stringify(vm.q),
				success : function(r) {
					// console.log(r.page);
					if (r.page) {
						vm.page = r.page;
						vm.superviseList = r.page.list;
						
						if (vm.page.currPage <= 1) {
							vm.page.currPage = 1;
							$("#example1_previous").addClass("disabled");
						} else {
							$("#example1_previous").removeClass("disabled");
						}
						if (vm.page.totalCount <= vm.page.currPage*10) {
							vm.page.currPage = Math.floor(vm.page.totalCount/10)+1;
							$("#example1_next").addClass("disabled");
						} else {
							$("#example1_next").removeClass("disabled");
						}
						$('#pagenum').val(vm.page.currPage)
					}else{
						alert("error");
					}
				},
				error : function(error) {
					alert("error");
					return false;
				}
			});
		},
		dimOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var objName = $(event.currentTarget).parent().prev().html();
			var selectedVal = $(event.currentTarget).find("option:selected");
			if (objName == "一级维度") {
				if (val == "") {
					vm.show.dim2 = [];
					vm.show.dim3 = [];
					vm.show.dim4 = [];
				} else {
					$.get(baseURL + "dimension/listfilterbyname?name=" + val, function(r) {
						vm.show.dim2 = r;
						vm.show.dim3 = [];
						vm.show.dim4 = [];
					})
				}
			} else if (objName == "二级维度") {
				if (val == "") {
					vm.show.dim3 = [];
					vm.show.dim4 = [];
				} else {
					$.get(baseURL + "dimension/listfilterbyname?name=" + val, function(r) {
						vm.show.dim3 = r;
						vm.show.dim4 = [];
					})
				}
			} else if (objName == "三级维度") {
				if (val == "") {
					vm.show.dim4 = [];
				} else {
					$.get(baseURL + "dimension/listfilterbyname?name=" + val, function(r) {
						vm.show.dim4 = r;
					})
				}
			} else if (objName == "四级维度") {
				vm.result.dim_type = selectedVal.attr("type");
				vm.result.dim_weight = selectedVal.attr("weight");
				// vm.result.dim_des=selectedVal.attr("description");
			}
		},
		carOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var objName = $(event.currentTarget).parent().prev().html();
			var selectedVal = $(event.currentTarget).find("option:selected");
			if (objName == "品牌") {
				if (val == "") {
					vm.show.car2 = [];
					vm.show.car3 = [];
				} else {
					$.get(baseURL + "carmodel/listfilterbyname?name=" + val, function(r) {
						vm.show.car2 = r;
						vm.show.car3 = [];
					})
				}
			} else if (objName == "系列") {
				if (val == "") {
					vm.show.car3 = [];
				} else {
					$.get(baseURL + "carmodel/listfilterbyname?name=" + val, function(r) {
						vm.show.car3 = r;
					})
				}
			} else if (objName == "型号") {

			}
			// vm.result.car_nickname=selectedVal.attr("nickname");
		},
		emotionOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var objName = $(event.currentTarget).parent().prev().html();
			var selectedVal = $(event.currentTarget).find("option:selected");
			if (objName == "情感词") {
				vm.result.emo_des = "";
				if (val == "") {
					vm.show.emotion2 = [];

					$("#emotion").removeAttr("disabled");
				} else {
					$.get(baseURL + "emotion/listfilterbyname?name=" + val, function(r) {
						vm.show.emotion2 = r;
						$("#emotion").attr("disabled","disabled");
					})
				}
			} else if (objName == "情感描述") {
			}
			vm.result.isFault = selectedVal.attr("isfault");
			vm.result.emotion_worth = selectedVal.attr("worth");
			var str = "";
			if (selectedVal.attr("worth") > 0) {
				str = "正面";
			} else if (selectedVal.attr("worth") < 0) {
				str = "负面";
			} else if (selectedVal.attr("worth") == 0) {
				str = "中立";
			}
			vm.result.emotion = str;
		},
		sentenceOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var selectedVal = $(event.currentTarget).find("option:selected");

			vm.result.sen_type = selectedVal.attr("type");
			vm.result.sen_worth = selectedVal.attr("sentenceWorth");
			vm.result.sen_keyword = selectedVal.attr("keyWord");
		},
		scenepatternOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var selectedVal = $(event.currentTarget).find("option:selected");

			vm.result.scene = selectedVal.attr("type");
			vm.result.buy_scene = selectedVal.attr("scene");
			vm.result.scene_key = selectedVal.attr("keyWord");
		},
		emoFilterOnChange : function(event) {
			var val = $(event.currentTarget).val();
			var emoFilter='null';
			if (val) {
				emoFilter=val;
			}
			$.get(baseURL + "emotion/level1list/"+emoFilter, function(r) {
				vm.show.emotion1 = r;
			})
		},
		clickQuery1 : function(e) {
			vm.q.post_time_from = '';
			vm.q.post_time_to = '';
			var val = e.currentTarget.innerText;
			vm.q.post_time = val;
			vm.page.currPage = 1;
			vm.queryPage();
		},
		clickQuery2 : function(e) {
			var val = e.currentTarget.innerText;
			if (val == '全部') {
				val = '';
			}
			vm.q.platform = val;
			vm.page.currPage = 1;
			vm.queryPage();
		},
		clickQuery3 : function(e) {
			var val = e.currentTarget.innerText;
			if (val == '全部') {
				val = '';
			}
			vm.q.source = val;
			vm.page.currPage = 1;
			vm.queryPage();
		},
		changeQueryContext : function(e) {
			vm.q.thread_context = e.currentTarget.value;
			vm.page.currPage = 1;
			vm.queryPage();
		},
		query : function() {
			vm.page.currPage = 1;
			vm.queryPage();
		},
		getCarData : function(parentId) {
			// 加载品牌树
			$.get(baseURL + "carmodel/listfilter/" + parentId, function(r) {
				vm.dim1 = r;
				vm.show.car1 = r;
			})
		},
		getDimData : function(parentId) {
			// 加载维度
			$.get(baseURL + "dimension/listfilter/" + parentId, function(r) {
				vm.show.dim1 = r;
			})
		},
		getEmotionData : function(parentId) {
			// 加载情感词
			$.get(baseURL + "emotion/listfilter/" + parentId, function(r) {
				vm.show.emotion1 = r;
			})
		},
		getScenepatternData : function() {
			// 加载场景句式库
			$.get(baseURL + "scenepattern/queryall/", function(r) {
				vm.show.scenepattern1 = r;
			})
		},
		getSentencepatternData : function() {
			// 加载 情感句式库
			$.get(baseURL + "sentencepattern/queryall", function(r) {
				vm.show.sentencepattern1 = r;
			})
		},
		initDateRang : function() {
			$('#daterange-btn').daterangepicker({
				maxDate : moment(), // 最大时间
				dateLimit : {
					days : 60
				}, // 起止时间的最大间隔
				// showDropdowns : true,
				// showWeekNumbers : false, //是否显示第几周
				// timePicker : true, //是否显示小时和分钟
				// timePickerIncrement : 60, //时间的增量，单位为分钟
				// timePicker12Hour : false,
				ranges : {
					// 'Today': [moment(), moment()],
					// 'Yesterday': [moment().subtract(1, 'days'),
					// moment().subtract(1, 'days')],
					'15天' : [ moment().subtract(14, 'days'), moment() ],
					'45天' : [ moment().subtract(44, 'days'), moment() ],
					'60天' : [ moment().subtract(59, 'days'), moment() ],
				// 'This Month': [moment().startOf('month'),
				// moment().endOf('month')],
				// 'Last Month': [moment().subtract(1,
				// 'month').startOf('month'), moment().subtract(1,
				// 'month').endOf('month')]
				},
				startDate : moment().subtract(29, 'days'),
				endDate : moment(),
				opens : 'right',
				format : 'YYYY-MM-DD', // 控件中from和to 显示的日期格式
				separator : ' to ',
				locale : {
					applyLabel : '确定',
					cancelLabel : '取消',
					fromLabel : '起始时间',
					toLabel : '结束时间',
					customRangeLabel : '选择时间',
					daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
					monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
					firstDay : 1
				}
			},

			function(start, end) {
				$('#daterange-btn span').html(start.format('YYYY-MM-DD') + ' 至 ' + end.format('YYYY-MM-DD'));
				vm.q.post_time_from = start.format('YYYY-MM-DD');
				vm.q.post_time_to = end.format('YYYY-MM-DD');
				vm.q.post_time = '';
				vm.page.currPage = 1;
				vm.queryPage();
			});
		},
		reload : function() {
			vm.showList = true;
			vm.queryPage();
		},
		setScrollTopSize : function() {
			$(document).scrollTop($("#hidScrollTopSize").val())
		},
		add : function() {
			vm.showList = false;
			vm.title = "新增";
			vm.sentencePattern = {};
		},
		update : function(event) {
			var id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";
			vm.getInfo(id)
		},
		getInfo : function(id) {
			$.get(baseURL + "supervise/info?docid=" + id, function(r) {
				vm.supervise = r.supervise;
				// console.log(vm.supervise);
			});
		},
		flipPages : function(i) {
			if (vm.page.currPage + i>5000) {
				alert("页数不可超过5000");
				return;
			}
			vm.page.currPage = vm.page.currPage + i;
			if (vm.page.currPage <= 1) {
				vm.page.currPage = 1;
				$("#example1_previous").addClass("disabled");
			} else {
				$("#example1_previous").removeClass("disabled");
			}

			parent.$('body,html').animate({scrollTop:50},0); 
			vm.queryPage();
		},
		toPage : function() {
			if ($('#pagenum').val()>5000) {
				alert("页数不可超过5000");
				return;
			}
			vm.page.currPage = $('#pagenum').val();
			if (vm.page.currPage <= 1) {
				vm.page.currPage = 1;
				$("#example1_previous").addClass("disabled");
			} else {
				$("#example1_previous").removeClass("disabled");
			}
			$('#pagenum').val(vm.page.currPage)
			parent.$('body,html').animate({scrollTop:50},0); 
			vm.queryPage();
		},
		saveOrUpdate : function(event) {
			var url = vm.result.index == -1 ? "supervise/add" : "supervise/update";
			$.ajax({
				type : "POST",
				url : baseURL + url,
				contentType : "application/json",
				data : JSON.stringify(vm.result),
				success : function(r) {
					if (r.code === 0) {
						alert('操作成功', function(index) {
							vm.reload();
						});
					} else {
						alert(r.msg);
					}
				},
				error : function(error) {
					// error错误描述信息
					alert("error")
					return false;
				}
			});
		},
		addNew : function(docid) {
			//获取滚动条高度
			$("#hidScrollTopSize").val($(document).scrollTop());
			$("#sentence").attr("disabled", false);
			$("#addNew2").attr("type", "hidden");
			vm.getInfo(docid)
			vm.title = "新增";
			vm.showList = false;
			vm.result = {
				docid : 0,
				index : 0,
				brand : '',
				series : '',
				model_num : '',
				first_dim : '',
				second_dim : '',
				third_dim : '',
				fourth_dim : '',
				emotion_word : '',
				emo_des : '',
				sen_demo : '',
				scene_demo : '',
			}
			$("#emoFilter").val("");
			vm.result.docid = docid;
			vm.result.index = -1;
			
			if (!vm.result.emotion_word||vm.result.emotion_word=='') {
				$("#emotion").removeAttr("disabled");
			}else{
				$("#emotion").attr("disabled","disabled");
			}
			
			parent.$('body,html').animate({scrollTop:0},100); 
		},
		addNew2 : function() {
			vm.title = "新增";
			$("#sentence").attr("disabled", false);
			$("#addNew2").attr("type", "hidden");
			$("#emoFilter").val("");
			vm.result.index = -1;
		},
		subInfo : function(docid, index) {
			//获取滚动条高度
			$("#hidScrollTopSize").val($(document).scrollTop());

			vm.title = "详情";
			vm.showList = false;
			$("#sentence").attr("disabled", true)
			$("#addNew2").attr("type", "button");
			$.get(baseURL + "supervise/info?docid=" + docid, function(r) {
				vm.supervise = r.supervise;
				vm.result = r.supervise.result[index];
				vm.result.docid = docid;
				vm.result.index = index;
				
				if (!vm.result.emotion_word||vm.result.emotion_word=='') {
					$("#emotion").removeAttr("disabled");
				}else{
					$("#emotion").attr("disabled","disabled");
				}
				$("#emoFilter").val("");
				parent.$('body,html').animate({scrollTop:0},100); 
			});

		},
		subDel : function(docid, index) {
			confirm('确定要删除吗？', function() {
				vm.result.docid = docid;
				vm.result.index = index;
				$.ajax({
					type : "POST",
					url : baseURL + "supervise/delete",
					contentType : "application/json",
					data : JSON.stringify(vm.result),
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功.', function(index) {
								vm.reload();
								vm.result.index = -1;
								vm.result.docid = 0;
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		addCopy : function(docid, index) {
			//获取滚动条高度
			$("#hidScrollTopSize").val($(document).scrollTop());
			vm.title = "新增";
			vm.showList = false;
			$("#sentence").attr("disabled", "disabled")
			$("#addNew2").attr("type", "hidden");
			$.get(baseURL + "supervise/info?docid=" + docid, function(r) {
				vm.supervise = r.supervise;
				vm.result = r.supervise.result[index];
				vm.result.docid = docid;
				vm.result.index = -1;

				$("#emoFilter").val("");
				if (!vm.result.emotion_word||vm.result.emotion_word=='') {
					$("#emotion").removeAttr("disabled");
				}else{
					$("#emotion").attr("disabled","disabled");
				}
				
				parent.$('body,html').animate({scrollTop:0},100); 
			});
		}
	}
});

var variantItem = Vue.extend({
	name : 'variant-item',
	props : {
		item : {},
		index : ''
	},
	template : [ '<div class="row h-2">', 
	             '<div class="col-md-1 h-5">{{ index+1 }}</div>', 
	             '<div class="content  col-md-11 h-0">', 
	             '<div class="row h-3 w-2">', 
	             '<div class="col-md-10">', 
	             '<p class="desc">{{item.thread_context}}（来源网站：{{item.website.indexOf("autohome")>-1?"汽车之家 - ":""}}{{item.website.indexOf("bitauto")>-1?"易车 - ":""}}{{item.website.indexOf("sinaauto")>-1?"新浪汽车 - ":""}}{{item.website.indexOf("pcauto")>-1?"太平洋汽车 - ":""}}{{item.website.indexOf("xcar")>-1?"爱卡汽车 - ":""}}{{item.website.indexOf("bbs")>-1?"论坛":""}}{{item.website.indexOf("koubei")>-1?"口碑":""}}，来源车型论坛：{{item.bbs_name}}{{item.user_name?"，发帖人："+item.user_name:""}}{{item.post_time?"，发帖时间："+item.post_time:""}}）</p>', 
	             '<p class="cont">{{item.thread_context}}（来源网站：{{item.website.indexOf("autohome")>-1?"汽车之家 - ":""}}{{item.website.indexOf("bitauto")>-1?"易车 - ":""}}{{item.website.indexOf("sinaauto")>-1?"新浪汽车 - ":""}}{{item.website.indexOf("pcauto")>-1?"太平洋汽车 - ":""}}{{item.website.indexOf("xcar")>-1?"爱卡汽车 - ":""}}{{item.website.indexOf("bbs")>-1?"论坛":""}}{{item.website.indexOf("koubei")>-1?"口碑":""}}，来源车型论坛：{{item.bbs_name}}{{item.user_name?"，发帖人："+item.user_name:""}}{{item.post_time?"，发帖时间："+item.post_time:""}}）</p>', 
	             '<a href="" class="pull-right"><i class="glyphicon glyphicon-triangle-bottom"></i> <span>展开</span></a>', 
	             '</div>', 
	             '<div class="col-md-2">', 
	             '<button v-if="hasPermission(\'supervise:update\')" type="button" @click="vm.addNew(item.id)" :docid="item.id" class="btn btn-primary btn-xs">', 
	             '<i class="glyphicon glyphicon-plus"></i> 新增', '</button>', '</div>', '</div>', '<div class="row w-6 h-6 hideScroll">',
				 '<div class="row h-1 w-1" style="line-height: 18px; overflow: hidden;" v-for="(tab,index1) in item.result" >', 
				 '<div class="col-md-2">{{index1+1}}.{{tab.sentence&&tab.sentence.length>20?tab.sentence.substr(0,20)+"……":tab.sentence}}</div>', 
				 '<div class="col-md-1">{{tab.brand?tab.brand:""}}{{tab.series?"-"+tab.series:""}}</div>', 
				 '<div class="col-md-2">{{tab.first_dim?tab.first_dim:""}}{{tab.second_dim?"-"+tab.second_dim:""}}{{tab.third_dim?"-"+tab.third_dim:""}}{{tab.fourth_dim?"-"+tab.fourth_dim:""}}{{tab.dim_type?"-"+tab.dim_type:""}}</div>', 
				 '<div class="col-md-2">{{tab.emotion_word?tab.emotion_word:""}}{{tab.emotion?"-"+tab.emotion:""}}{{tab.emotion_worth?"-"+tab.emotion_worth:""}}{{tab.isFault==1?"-故障":""}}{{tab.isFault==0?"-非故障":""}}</div>',
				 '<div class="col-md-1">{{tab.sen_type?tab.sen_type:""}}{{tab.sen_worth?tab.sen_worth:""}}</div>', 
				 '<div class="col-md-2">{{tab.scene?tab.scene:""}}</div>', 
				 '<div class="col-md-2">',
				 '<button type="button" @click="vm.subInfo(item.id,index1)" class="btn btn-info btn-xs" >详</button>&nbsp;', 
				 '<button v-if="hasPermission(\'supervise:update\')" type="button" @click="vm.subDel(item.id,index1)"  class="btn btn-danger btn-xs">删</button>&nbsp;', 
				 '<button v-if="hasPermission(\'supervise:update\')" type="button" @click="vm.addCopy(item.id,index1)" class="btn btn-primary btn-xs">增</button>', 
				 '</div>', 
				 '</div>',
				 '</div>', 
				 '</div>', 
				 '</div>'
				 ].join('')
});

// 注册菜单组件
Vue.component('variantItem', variantItem);
